<?php

//defined('APP_PATH') or die();
class FrontController {

    protected static $objInstance = null;
    private $controllerPhrase;
    private $controllerCommand;
    private $activePage;

    public static function getInstance() {
        if (is_null(self::$objInstance)) {
            self::$objInstance = new self();
        }

        return self::$objInstance;
    }

    function __construct() {
        //$this->routeMap['users']='users.php';        
    }

    /*
      function initSecondaryControler() {
      if (isset($_SESSION["UserAccess"])) {

      //$layout->putPage(&$this->activePage);
      if (!isset($this->activePage))
      switch ($this->getPhrase()) {
      case '':
      $this->activePage = new Layout();
      break;
      case 'home':
      case 'index.php':
      $this->activePage = new HomePage();
      break;
      case 'client':
      $this->activePage = new ClientPage();
      break;
      case 'users':
      $this->activePage = new UsersPage();
      break;
      case 'profile':
      $this->activePage = new ProfilePage();
      break;
      case 'mailing':
      $this->activePage = new MailingPage();
      break;

      case 'logout':
      include 'logout.php';
      break;
      default :
      $this->activePage = new ErrorController($this->getPhrase());
      }
      } else {
      $this->activePage = new LoginPage();
      if (isset($_REQUEST['login']) && isset($_REQUEST['password']))
      $this->activePage->login();
      $this->activePage->display();
      }
      if (isset($this->controllerCommand)) {
      $this->activePage->executeAction($this->controllerCommand);
      }

      $this->activePage->display();
      unset ($this->activePage);
      }
     */

    function initSecondaryControler() {
        if (isset($_SESSION["UserAccess"])) {
            $this->activePage = new Layout();
        }
    }

    function handleGridRequest() {
        if (isset($_REQUEST['changecriteria']))
            $_SESSION['grid']->setCriteria($_REQUEST);
        $_SESSION['grid']->displayPage($_SESSION['page']);
    }

    function handleSiteRequest() {
        $this->activePage = ControllerFactory::build($_REQUEST['site']);
        if (isset($_REQUEST['action']))
            $this->activePage->executeAction($_REQUEST['action']);
        else
            $this->activePage->display();
    }

    function parse($param) {
        Propel::init('./model/wincon_mailing_db/build/conf/wincon_mailing_db-conf.php');
        #var_dump($_REQUEST);
        if (isset($_SESSION["UserAccess"])) {
            if (isset($_REQUEST['grid'])) {
                $this->handleGridRequest();
            } else
            if (isset($_REQUEST['site'])) {
                $this->handleSiteRequest();
            } else {

                $layout = Layout::getInstance();
                $layout->display();
            }
        } else {
            $login = new LoginPage();
            if (isset($_REQUEST['login']) && isset($_REQUEST['password']))
                $login->login();
            $login->display();
        }
        /*
          if (isset($_REQUEST['source']) && isset($_REQUEST['action'])) {
          $this->controllerPhrase = $_REQUEST['source'];
          $this->controllerCommand = $_REQUEST['action'];
          } else {

          $array_var = explode("/", $param);
          $array_var = array_unique($array_var);

          $this->controllerPhrase = $array_var[1];
          $this->controllerCommand = $array_var[2];

          }

         */
    }

    function getPhrase() {
        return $this->controllerPhrase;
    }

    function getCommand() {
        return $this->controllerCommand;
    }

}

?>
